<?php
/**
 * The form for updating a material.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-20
 */
require_once "login_info.php";
require_once "materials.php";
loginInfo::update_login();
?>

<html>
	<head>
		<title>Opol - Update Material</title>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php' ?>
		<br>
<?php
if ($_POST) {
	if (isset($_POST['mat_id'])) {
		$db = new database();

		// Update the material's information, and move on.
		$mat = new material ((int)$_POST['mat_id']);
		$mat->init_from_post ($_POST);
		$result = $mat->update_database_by_id ($db);

		// Offer the option to move on.

		if (!$result)
			echo "You do not have sufficient permissions to perform this action.<br>\n";
		else
			echo "Database Successfully Updated!<br>\n";

		echo "<a href=\"/\">Back to main menu.</a><br>\n";

	} else {

		$mat_id = $_POST['name'];
		if (!$mat_id) {
			//echo "Please specify the material's name.<br>\n";
		} else {
			$db = new database();

			$mat = new material ($mat_id);
			$ret = $mat->init($db);
			if (!$ret) {
				echo "Error<br>\n";
			}

			$db->close ();
		}
	}

	$name 		= $mat->name;
	$quantity 	= $mat->qty;
	$unit 		= $mat->unit;
	$price 		= $mat->price;


} else {
	header("location: update_entry.php?type=material");
}

?>
		<form name="material" action="" method="post">
			<input type="hidden" name="mat_id" value="<?php print $mat_id;?>">
			<table>
				<tr>
					<td><label>Name:</label></td>
					<td><input type="text" name="name" value="<?php print $name;?>"></td>
			   </tr>
			   <tr>
					<td><label>Quantity:</label></td>
					<td><input type="text" name="qty" value="<?php print $quantity;?>"></td>
			   </tr>

			   <tr>
					<td><label>Price:</label></td>
					<td><input type="text" name="price" value="<?php print $price;?>"></td>
			   </tr>

			   <tr>
					<td><label>Units:</label></td>
					<td><input type="text" name="unit" value="<?php print $unit;?>"></td>
			   </tr>
	
			   <tr>
					<td><input type="submit" value="Update"></td>
			   </tr>
			</table>
		</form>
	</body>
</html>
